package com.tran.app.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tran.app.MyApp
import com.tran.app.dao.AppDatabase
import com.tran.app.data.Course
import com.tran.app.data.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CourseScreenVM:ViewModel() {

    fun update(cid: Int) {
        viewModelScope.launch {
            withContext(Dispatchers.IO){
                val user = AppDatabase.getInstance(MyApp.instance).CourseSelectionDao().getStudentsByCourseId(cid)
                _uidata.update {
                    user
                }
            }
        }
    }

    private val _uidata = MutableStateFlow<List<UserInfo>>(emptyList())
    val uidata: StateFlow<List<UserInfo>> = _uidata.asStateFlow()

}